const express = require("express");
var nodeExcel = require("excel-export");
const router = express.Router();
// 导入用户路由处理函数
const userHandler = require("../rooterhandler/user")
// 导入验证数据表单的中间件
const expressJoi = require("@escook/express-joi");
// 导入需要的验证规则
const {regLoginSchema} = require("../schema/user")
// 新用户注册
router.post("/register",expressJoi(regLoginSchema),userHandler.regUser)
// 新用户登录
router.post("/login",expressJoi(regLoginSchema),userHandler.loginUser)
// sendfile
router.get("/getfile",userHandler.getFile);
let downlaodExcel=function (res,top,element,data,fileName) {
    var conf ={};
    conf.cols=[];
    for(var i=0;i<top.length;i++){
        var tits ={};
        tits.caption = top[i];
        tits.type='String';
        conf.cols.push(tits);
    }
    var datas = [["1","2"],["3","4"]];
    conf.rows=datas;
    var result =nodeExcel.execute(conf);
    fileName=encodeURIComponent(fileName);
    res.setHeader('Content-Type', 'application/vnd.openxmlformats');
    res.setHeader("Content-Disposition", "attachment; filename=" + fileName+".xlsx");
    res.end(result, 'binary');
};

router.get("/exportexcel",async function (req,res) {
	try{
        var top =['工号','姓名'];//表头
        var element = ['u_number','u_name'];//字段
        var data = [[1,2]
        ];//数据
        var fileName = '人员清单';//文件名
        downlaodExcel(res,top,element,data,fileName);//直接调用导入方法
	}catch(err){
		console.log(err);
	}
})
module.exports = router;